package com.thread.threadfirst.javaBase.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Comparator
 */
public class ComparatorTest {

    private static class Role{
        private Long Id;
        private String name;
        private Integer gold;

        public Role(Long id, String name, Integer gold) {
            Id = id;
            this.name = name;
            this.gold = gold;
        }

        public Long getId() {
            return Id;
        }

        public void setId(Long id) {
            Id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getGold() {
            return gold;
        }

        public void setGold(Integer gold) {
            this.gold = gold;
        }

        @Override
        public String toString() {
            return "Role{" +
                    "Id=" + Id +
                    ", name='" + name + '\'' +
                    ", gold=" + gold +
                    '}';
        }
    }

    public static void main(String[] args) {
        List<Integer> list=Arrays.asList(1,4,7,3,2);
        list.sort(Comparator.naturalOrder());//升序
        System.out.println(list);

        list.sort(Comparator.reverseOrder());//降序
        System.out.println(list);

        List<Role> roleList=new ArrayList<>();
        roleList.add(new Role(5L,"luffy",50));
        roleList.add(new Role(3L,"zoro",30));
        roleList.add(new Role(7L,"sanji",88));

        roleList.sort(Comparator.comparingLong(Role::getId));//升序
        System.out.println(roleList);

        roleList.sort(Comparator.comparingLong(Role::getId).reversed());//降序
        System.out.println(roleList);

        roleList.sort(Comparator.comparingLong(Role::getId).thenComparingInt(Role::getGold).reversed());
        System.out.println(roleList);
        //输出：[Role{Id=7, name='sanji', gold=88}, Role{Id=5, name='luffy', gold=50}, Role{Id=3, name='zoro', gold=30}]，显然两个一起排序 结果不靠谱

    }
}
